# -*- encoding: utf-8 -*-
'''
@File    :   4.py
@Time    :   2022/11/09 23:11:27
@Author  :   Cms 
@Version :   1.0
@Contact :   2267000140@qq.com
@WebSite    :   www.baidu.com
'''


'''
4 题目要求：
 在当前目录新建目录img, 里面包含10个文件, 10个文件名各不相同(X4G5.png)
 将当前img目录所有以.png结尾的后缀名改为.jpg.
'''

# here put the import lib
import os

# 获取到所有的旧文件的名称
oldname_list = os.listdir(r"homework3\\img\\")
# 将旧名称的png替换成jpg
newname_list = []
for i in oldname_list:
    temp = i.replace('png', 'jpg')
    newname_list.append(temp)
# print(newname_list)

# 文件的目录
dirname = "homework3\\img\\"

# 旧文件的目录
oldname_lists = []
for i in oldname_list:
    i = dirname + i
    oldname_lists.append(i)
    # print(i)
print(oldname_lists)

# 新文件的目录
newname_lists = []
for i in newname_list:
    i = dirname + i
    newname_lists.append(i)
print(newname_lists)

# 替换名称
for i in range(len(newname_lists)):
    os.rename(oldname_lists[i], newname_lists[i])
